/*
 * Copyright 2010 WithOne Co. Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jp.co.withone.osgi.gadget.fileconnector;

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.imageio.ImageIO;

import jp.co.withone.osgi.gadget.pictureviewer.PictureController;

/**
 * The PictureControllerImpl class read picture file on local directory and that
 * change byte array.
 * 
 * @author funakky
 */
public class PictureControllerImpl implements PictureController {

	private int index;

	/**
	 * Read picture file on local directory and return picture byte array.
	 * This method is synchronized on this PictureControllerImpl object.
	 * 
	 * @return picture
	 * @see jp.co.withone.osgi.gadget.pictureviewer.PictureController#getPicture()
	 */
	@Override
	public synchronized byte[] getPicture() {
		final File dir = new File(FileConnectorSettingView.dirPath);
		if (!dir.isDirectory()) {
			return null;
		}
		final File[] files = dir.listFiles();
		if (files.length == 0) {
			return null;
		}
		for (int i = index; i < files.length; i++) {
			final File file = files[i];
			if (check(file)) {
				index = i + 1;
				return getImageByte(file);
			}
		}
		if (index > files.length) {
			index = files.length;
		}
		for (int i = 0; i < index; i++) {
			final File file = files[i];
			if (check(file)) {
				index = i + 1;
				return getImageByte(file);
			}
		}
		return null;

	}

	private boolean check(File file) {
		if (!file.isFile()) {
			return false;
		}

		try {
			final BufferedImage readImage = ImageIO.read(new FileInputStream(
					file));
			if (readImage == null) {
				return false;
			}
		} catch (FileNotFoundException e) {
			return false;
		} catch (IOException e) {
			return false;
		}
		return true;
	}

	private byte[] getImageByte(File file) {
		try {
			final ByteArrayOutputStream baos = new ByteArrayOutputStream();
			final BufferedInputStream bis = new BufferedInputStream(
					new FileInputStream(file));
			final byte[] b = new byte[1024];
			while (true) {
				final int count = bis.read(b);
				if (count == -1) {
					break;
				}
				baos.write(b, 0, count);
			}
			return baos.toByteArray();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

}
